/**
 * 
 */
package de.nak.fireDepartment.administrationAction;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.springframework.web.struts.ActionSupport;

import de.nak.fireDepartment.actionForms.DepartmentForm;
import de.nak.fireDepartment.exceptions.DepartmentAlreadyExistsException;
import de.nak.fireDepartment.exceptions.ImageUploadException;
import de.nak.fireDepartment.model.Department;
import de.nak.fireDepartment.service.DepartmentService;
import de.nak.fireDepartment.service.ImageUploadService;
import de.nak.fireDepartment.service.LoginService;

/**
 * Action class for creating a new department.
 * @author Sven Redelin
 */
public class CreateEditDepartmentAction extends ActionSupport {
	
	/** 
	 * This method creates or updates a department
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return actionforward
	 * @throws Exception
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// ActionForm => DepartmentForm
		DepartmentForm departmentForm = (DepartmentForm) form;
		// Get the department from departmentform
		Department department = departmentForm.getDepartment();
		// Clear ErrorCode to display
		departmentForm.setErrorCode("");
		
		// get the department and image service
		DepartmentService departmentService = (DepartmentService)getWebApplicationContext().getBean("departmentService");
		ImageUploadService imageUploadService = (ImageUploadService)getWebApplicationContext().getBean("imageUploadService");
		
		// Validate the user-login
		LoginService loginService = (LoginService)getWebApplicationContext().getBean("loginService");
		if(!loginService.checkUserIsValidated()){
			return mapping.findForward("missinglogin");
		}
		
		// get the formfile
		FormFile aFile = departmentForm.getaDepartmentImageFile();
        String filePath = "";
        // if there are errors while uploading, the message should be put into the errorcode
        try{
        	filePath = imageUploadService.uploadFile(aFile, ImageUploadService.DEPARTMENT, getServletContext());
        }catch(ImageUploadException ex){
        	departmentForm.setErrorCode(ex.getMessage());
        }
        
        // if the department id is set you have to update the car, otherwise create a new department
        try{
	        if(department.getDeptId() > 0){
				departmentService.updateDepartment(department.getDeptId(), department.getCity(), department.getZipcode(), department.getStreet(), department.getHouseNumber(), department.getTelephoneNumber(), department.getTelefaxNumber(), department.getHomepageURL(), department.geteMail(), department.getDescriptionText(), department.getChief(), department.getCoChief(), department.getMembers(), filePath, departmentForm.getSelectedCarIds());
			}else{
				departmentService.createDepartment(department.getCity(), department.getZipcode(), department.getStreet(), department.getHouseNumber(), department.getTelephoneNumber(), department.getTelefaxNumber(), department.getHomepageURL(), department.geteMail(), department.getDescriptionText(), department.getChief(), department.getCoChief(), department.getMembers(), filePath, departmentForm.getSelectedCarIds());
			}
        }catch(DepartmentAlreadyExistsException ex){
        	departmentForm.setErrorCode(ex.getMessage());
        }
        
		// Reload list
        departmentForm.setSelectedKey(null);
		departmentForm.setDepartments(departmentService.listDepartments());
		// Return result
		return mapping.findForward("success");
	}
}
